﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;
using System.Web.Configuration;

namespace WXYW
{
    public partial class UpgradeConfirmApplication : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitialPage();
            }
        }

        private void InitialPage()
        {
            UpgradeService service = new UpgradeService();
            UpgradeForm upForm = service.GetProductDetailForm(long.Parse(Request.QueryString[Constants.QUERYSTR_UPGRADE_ID]));
            productTypeLabel.Text = upForm.ApplicationProduct.ProductType.ProductTypeName;
            serialNumberLabel.Text = upForm.ApplicationProduct.ProductSerialNumber;
            problemTypeLabel.Text = upForm.ApplicationProduct.ProblemType.Name;
            decriptionLabel.Text = upForm.ApplicationProduct.ProductProblemDescription;
            appearanceLabel.Text = upForm.ApplicationProduct.ProductAppearance;
            repairTypeLabel.Text = upForm.ApplicationProduct.ProductRepairType;
            examineResultLabel.Text = upForm.ApplicationProduct.Remark;
            upgradeReason.Text = upForm.UpgradeReason;
            serialLabel.Text = upForm.ApplicationProduct.ProductSerialNumber;
            rmacodeLabel.Text = upForm.Code;
            applicationRMALbl.Text = upForm.ApplicationProduct.ApplicationForm.ApplicationCode; 
        }

        protected void disAgreeLinkButton_Click(object sender, EventArgs e)
        {
            disagreeModalPopupExtender.Show();
        }

        // 同意升级
        protected void agreeLinkButton_Click(object sender, EventArgs e)
        {
            try
            {
                UpgradeService service = new UpgradeService();
                UpgradeForm upForm = service.GetUpgradeFormByID(long.Parse(Request.QueryString[Constants.QUERYSTR_UPGRADE_ID]));
                upForm.Status = "升级-同意待发货";
                upForm.DealWithTime = DateTime.Now;
                if (!upForm.ApplicationProductReference.IsLoaded)
                {
                    upForm.ApplicationProductReference.Load();
                }
                //upForm.ApplicationProduct.Status = "升级-同意待发货"; //2013.8.31 注释
                service.Update(upForm);


                // 邮件通知
                if (!upForm.UserReference.IsLoaded)
                {
                    upForm.UserReference.Load();
                }
                string body = string.Format(WebConfigurationManager.AppSettings["UpgradeApproveApplication"], upForm.Code);
                EmailService.SendEmail(upForm.User.UserEmail, "升级申请通过了", body);
                ClientScript.RegisterStartupScript(this.GetType(), "tip", "alert('你已经同意了该升级申请');location.href='UpgradeNeedApprove.aspx';", true);
            }
            catch (Exception ex)
            {
                LogUtility.LogUtilityIntance.LogException(ex, "UpgradeConfirmApplication.aspx::agreeLinkButton_Click");
                throw ex;
            }
        }

        // 不同意升级
        protected void saveLinkButton_Click(object sender, EventArgs e)
        {
            try
            {
                UpgradeService service = new UpgradeService();
                UpgradeForm upForm = service.GetUpgradeFormByID(long.Parse(Request.QueryString[Constants.QUERYSTR_UPGRADE_ID]));
                upForm.Status = "升级-不同意";
                upForm.DealWithTime = DateTime.Now;
                upForm.CompleteTime = DateTime.Now;
                upForm.Suggestion = reasonAndSolutinTextBox.Text;
                if (!upForm.ApplicationProductReference.IsLoaded)
                {
                    upForm.ApplicationProductReference.Load();
                }
                if (!upForm.UserReference.IsLoaded)
                {
                    upForm.UserReference.Load();
                }
                upForm.ApplicationProduct.Status = "升级驳回-待修";
                upForm.ApplicationProduct.User = upForm.User;
                service.Update(upForm);

                string body = string.Format(WebConfigurationManager.AppSettings["UpgradeDeclineApplication"], upForm.Code);
                EmailService.SendEmail(upForm.User.UserEmail, "升级申请被拒绝", body);
                ClientScript.RegisterStartupScript(this.GetType(), "tip", "alert('你已经拒绝了该升级申请');location.href='UpgradeNeedApprove.aspx';", true);
            }
            catch (Exception ex)
            {
                LogUtility.LogUtilityIntance.LogException(ex, "UpgradeConfirmApplication.aspx::saveLinkButton_Click");
                throw ex;
            }
        }
    }
}
